/**
 *   处理检测蓝牙开启 true为开启 fsle为关闭
 */
export function openBluetooth() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success(res) {
				if (!res.authSetting['scope.bluetooth']) {
					uni.showModal({
						title: '提示',
						content: '当前小程序未开启蓝牙授权，是否前往授权？',
						success(res) {
							if (res.confirm) {
								uni.openBluetoothAdapter();
								resolve(true)
							} else if (res.cancel) {
								resolve(false)
							}
						},
					});
				} else {
					uni.openBluetoothAdapter({
						success: (res) => {
							resolve(true)
						},
						fail: (res) => {
							//如果手机上的蓝牙没有打开，可以提醒用户
							resolve(false)
							uni.showModal({
								title: '提示',
								content: '请打开手机蓝牙',
								showCancel: false,
							});
							uni.closeBluetoothAdapter({});
						},
					});
				}
			},
		});
	})

}
// 添加设备
export const addBluetoothDevice = (plugin) => {
	return new Promise((resolve, reject) => {
		// 监听“初始化完成”事件
		plugin.on('ready', function(res) {
			console.log('plugin is on ready', res);
			plugin
				.addBluetoothDevice()
				.then(function(res) {
					console.log('addBluetoothDevice', res);
					plugin.validateLockInitialInfo({
						isSuccess: true
					}).then(res => {
						console.log("validateLockInitialInfo", res)
					})
					// uni.hideLoading();
					resolve(res);
				})
				.catch(function(err) {
					console.log('addBluetoothDevice-->err', err);
					// uni.hideLoading();
					reject(err);
				});
		});
		// 监听“运行错误”事件
		plugin.on('error', function(err) {
			console.log('plugin is on error -->', err);
			reject(err);
		});
	});
}

// 蓝牙开锁
export const onOpenLock = (MyPlugin,params) => {
	console.log("进入onOpenLock")
	return new Promise((resolve, reject) => {
		console.log("进入onOpenLock——Promise")
		MyPlugin.on('ready', function() {
			MyPlugin
				.openLock()
				.then((res) => {
					console.log("openLock res -->", res);
					resolve(res);
				})
				.catch((err) => {
					console.log("openLock res -->", res);
					reject(res);
				});
		});
	});
}
